package com.yuerlive.rainbow.widget.game;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.RainbowApp;
import com.yuerlive.rainbow.entity.Poker;
import com.yuerlive.rainbow.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoyi on 2017/7/4.
 */

public class PokerGroup extends FrameLayout{
    private int pokerCount  = 5;
    private int pokerBack = R.drawable.poker_niuniu_back;
    private ImageView ivResult;
    private List<PokerView> pokerViews = new ArrayList<>();
    private List<Poker> pokers = new ArrayList<>();
    public List<int[]> pokerLocations = new ArrayList<>();
    public PokerGroup(@NonNull Context context) {
        this(context,null);
    }

    public PokerGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PokerGroup(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.PokerGroup);
        pokerCount = array.getInt(R.styleable.PokerGroup_count,5);
        pokerBack = array.getResourceId(R.styleable.PokerGroup_pockerBack,pokerBack);
        array.recycle();
        setClipChildren(false);
        setClipToPadding(false);
        initPokerChildren(context);

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                for (int i = 0; i < pokerViews.size(); i++) {
                    View poker = pokerViews.get(i);
                    int[] location = new int[2];
                    poker.getLocationOnScreen(location);
                    pokerLocations.add(location);
                }
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

        hide();
    }

    private void initPokerChildren(Context context) {
        int childMargins = DeviceUtils.dp2px(context,15);
        pokerViews.clear();
        for (int i=0;i<pokerCount;i++){
            PokerView child = new PokerView(context);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(childMargins*i,0,0,0);
            child.setPokerBackRes(pokerBack);
            addView(child,params);
            pokerViews.add(child);
        }
        ivResult = new ImageView(context);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.BOTTOM;
        ivResult.setImageResource(R.drawable.niuniu_0);
        addView(ivResult,params);
        ivResult.setVisibility(GONE);
    }

    public void showBack() {
        for (PokerView pokerView : pokerViews) {
            pokerView.setVisibility(VISIBLE);
            pokerView.showBlack();
        }
    }

    public void hide() {
        for (PokerView pokerView : pokerViews) {
            pokerView.setVisibility(INVISIBLE);
        }
    }

    public void setPokers(List<Poker> pokers) {
        this.pokers.clear();
        this.pokers = pokers;
        for (int i = 0; i < pokerViews.size(); i++) {
            pokerViews.get(i).setPokerValue(pokers.get(i));
            pokerViews.get(i).show();
        }
    }

    public void setResult(int result) {
        String resEnd = "0";
        if (result < 10) {
            resEnd = String.valueOf(result);
        }
        switch (result) {
            case 10:
                resEnd = "nn";
                break;
            case 11:
                resEnd = "yn";
                break;
            case 12:
                resEnd = "jn";
                break;
            case 13:
                resEnd = "zd";
                break;
            case 14:
                resEnd = "wxn";
                break;
        }
        int resId = getResources().getIdentifier("niuniu_" + resEnd, "drawable", RainbowApp.getContext().getPackageName());
        ivResult.setImageResource(resId);
        ivResult.setVisibility(VISIBLE);
    }

    public void clean() {
        ivResult.setVisibility(GONE);
        hide();
    }
}
